package cloud.xlh.my_system.utils;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/12/22 10:49
 * @ Description:
 */
public class DynamicClassLoader extends ClassLoader{

    @Override
    protected Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> aClass = findLoadedClass(name);
            if (aClass == null) {
                if (name.startsWith("java.") || name.startsWith("javax")) {
                    try {
                         aClass = getSystemClassLoader().loadClass(name);
                    } catch (Exception e) {

                    }
                } else {
                    try {
                         aClass = this.findClass(name);
                    } catch (Exception e) {

                    }
                    if (aClass == null) {
                        if (getParent() != null) {
                            aClass = getParent().loadClass(name);
                        } else {
                            aClass = getSystemClassLoader().loadClass(name);
                        }
                    }
                }
            }
            if (resolve) {
                resolveClass(aClass);
            }
            return aClass;
        }
    }



}
